package com.fh.modules.ems.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.ems.entity.EmsHomePushProduct;
import com.fh.modules.ems.service.IEmsHomePushProductService;
import com.fh.modules.ems.vo.PushProductQryVo;
import com.fhMall.request.QueryEmsHomePushProductRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Title: Controller
 * @Description: 首页推荐商品
 * @author： code generator
 * @date：   2019-09-28
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("首页推荐商品")
public class EmsHomePushProductController {
	@Autowired
	private IEmsHomePushProductService emsHomePushProductService;

	@GetMapping(value = "/ems/emsHomePushProduct/list")
	@ApiOperation("分页列表查询")
	@AutoLog(value="首页推荐商品分页查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryPageList(EmsHomePushProduct emsHomePushProduct,
								@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								HttpServletRequest req) {
		QueryWrapper<EmsHomePushProduct> queryWrapper = new QueryWrapper<EmsHomePushProduct>();
		emsHomePushProduct.setDelFlag(CommonConstant.DEL_FLAG_0);
		QueryWrapperUtil.initQueryWrapper(queryWrapper, emsHomePushProduct);
		Page<EmsHomePushProduct> page = new Page<EmsHomePushProduct>(pageNo, pageSize);
		IPage<EmsHomePushProduct> pageList = emsHomePushProductService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 /**
	  * 移动端查询推荐商品
	  * @return
	  */
	 @GetMapping(value = "/ems/emsHomePushProduct/queryAll",consumes = "application/json")
	 @ApiOperation("移动端查询推荐商品")
	 @AutoLog(value = "移动端查询推荐商品", logType = CommonConstant.LOG_TYPE_QUERY)
	 public Result queryAll(@RequestBody PushProductQryVo vo) {
		 return Result.okPage(emsHomePushProductService.queryPushProductPage(vo));
	 }


	/**
	  *   添加
	 * @param emsHomePushProduct
	 * @return
	 */
	@PostMapping(value = "/ems/emsHomePushProduct/add")
	@ApiOperation("添加")
	@AutoLog("添加首页推荐商品")
	public Result add(@RequestBody EmsHomePushProduct emsHomePushProduct) {
		emsHomePushProduct.setDelFlag(CommonConstant.DEL_FLAG_0);
		emsHomePushProduct.setAddressType(1);
		emsHomePushProductService.save(emsHomePushProduct);
		return Result.ok("添加成功");
	}
	
	/**
	  *  编辑
	 * @param emsHomePushProduct
	 * @return
	 */
	@PutMapping(value = "/ems/emsHomePushProduct/edit")
	@ApiOperation("修改")
	@AutoLog("修改首页推荐商品")
	public Result edit(@RequestBody EmsHomePushProduct emsHomePushProduct) {
		boolean ok = emsHomePushProductService.updateById(emsHomePushProduct);
		return ok?Result.ok("修改成功"):Result.error("修改失败");
	}

	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/ems/emsHomePushProduct/deleteBatch")
	@ApiOperation("批量删除")
	@AutoLog("批量删除首页推荐商品")
	public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		emsHomePushProductService.deleteBatchEntityByDelFlag(ids);
		return Result.ok("批量删除成功");
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/ems/emsHomePushProduct/queryById")
	@ApiOperation("通过id查询")
	@AutoLog(value="首页推荐商品通过id查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryById(@RequestParam(name="id",required=true) String id) {
		EmsHomePushProduct emsHomePushProduct = emsHomePushProductService.getById(id);
		return Result.ok(emsHomePushProduct);
	}

}
